﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using EbGameBaseConfig;
//using System.Threading.Tasks;

namespace CSharpTestApp
{
    class Program
    {
        static void Main(string[] args)
        {


            //FindDllReferences();
            //DoJsonTest();
            //DoJsonTest1();

            DoCmdExecute();
        }

        //执行其它进程，并传递命令行参数
        public static void DoCmdExecute()
        {
            ExecuteCmd cmd = new ExecuteCmd();
            cmd.ExecuteApp();
        }


        //查找DLL中依赖的DLL，并输出到文件中
        public static void FindDllReferences()
        {
            FileDirManager fileDirMgr = new FileDirManager();
            string sourceDirUnity = @"F:\Code\Edbox\Edbox_2017\PlayerProject\Assets\Plugins";
            string outfileUnity = @"G:\DllReference\outfileUnity.txt";
            fileDirMgr.FindDllReferences(sourceDirUnity, outfileUnity);

            string sourceDirVS = @"F:\Code\Edbox\Edbox_2017\Edbox_Client\libs";
            string outfileVS = @"G:\DllReference\outfileVS.txt";
            fileDirMgr.FindDllReferences(sourceDirVS, outfileVS);
        }



        public static void DoJsonTest1()
        {
            string configPath = @"E:\WorkSpace\code\Test\Edbox2017\EdboxUpdate2017\EdboxUpdate2017\Config\GameBaseConfig.json";
            EbGameBaseConfigMgr.Instance.Init(configPath);
            EbGameBaseConfigMgr.Instance.SetValue("AppName", "111");
            EbGameBaseConfigMgr.Instance.SaveFile();
        }

        public static void DoJsonTest()
        {
            int a = ClassLibrary1.Class1.PrintSum();

            string filePath = @"D:\Games\XYClient\version.xml";
            string dirName = System.IO.Path.GetDirectoryName(filePath);
            string fullpath = System.IO.Path.GetFullPath(filePath);

            string apath = @"G:\a";
            string bpath = @"G:\b";
            CopyDirFiles(apath, bpath);

            double dd = 0.0;
            string strdd = dd.ToString();

            ///Coroutine Test协程测试
            #region Coroutine Test协程测试
            //             CustomCollection cc = new CustomCollection();
            // 
            //             IEnumerator enumerator = cc.GetEnumerator();
            //             while (true)
            //             {
            //                 bool canMoveNext = enumerator.MoveNext();
            //                  Console.WriteLine("canMoveNext:" + canMoveNext);
            //                  if (!canMoveNext)
            //                      break;
            //                  Object obj = enumerator.Current;
            //                  Console.WriteLine("current obj:" + obj);
            //              }
            //             //             foreach (String word in cc)
            //             //             {
            //             //                 Console.WriteLine("word:" + word);
            //             //             }
            // 
            #endregion

            #region Newtonsoft.Json的测试代码
            var obj = 10.4;

            string str = JsonConvert.SerializeObject(obj);

            float desFloat = JsonConvert.DeserializeObject<float>(str);

            MyClass mycl = new MyClass();
            mycl.MyMemb1 = "ttata";
            mycl.MyMemb2 = 11.9f;

            MyClass myc2 = new MyClass();
            myc2.MyMemb1 = "dddd";
            myc2.MyMemb2 = 20.0f;

            MyClass myc3 = new MyClass();
            myc3.MyMemb1 = "3333";
            myc3.MyMemb2 = 30.0f;

            mycl = myc2;
            myc2 = myc3;

            string strMycl = JsonConvert.SerializeObject(mycl);

            MyClass desMycl = JsonConvert.DeserializeObject<MyClass>(strMycl);
            MyClass desMyc2 = JsonConvert.DeserializeAnonymousType<MyClass>(strMycl, myc2);

            var definition = new { Name = "" };

            string json1 = @"{'Name':'James'}";
            var customer1 = JsonConvert.DeserializeAnonymousType(json1, definition);

            Console.WriteLine(customer1.Name);
            // James

            string json2 = @"{'Name':'Mike'}";
            var customer2 = JsonConvert.DeserializeAnonymousType(json2, definition);

            Console.WriteLine(customer2.Name);
            // Mike

            var strobj = 10.4;

            string strMemb2 = JsonConvert.SerializeObject(strobj);

            object objMember2 = null;
            NewtonsoftJsonTest.GetMemberRef(mycl, "MyMemb2", ref objMember2);
            Type objtype = objMember2.GetType();

            //objMember2 = JsonConvert.DeserializeAnonymousType(strMemb2, objMember2);
            objMember2 = JsonConvert.DeserializeObject(strMemb2, objtype);
            NewtonsoftJsonTest.SetMemberValue(mycl, "MyMemb2", objMember2, objtype);


            //Class Test

            MyClass2 myClass2 = new MyClass2();
            myClass2.strMember1 = "111";
            myClass2.fMember2 = 30.0f;
            string strMyClass2 = JsonConvert.SerializeObject(myClass2);



            MyClass1 myClass1 = new MyClass1();
            myClass1.MyMember1 = "myClass1.MyMember1";
            myClass1.MyMember2.strMember1 = "myClass1.MyMember2.strMember1";
            myClass1.MyMember2.fMember2 = 205;

            object refMyClass2 = null;
            NewtonsoftJsonTest.GetMemberRef(myClass1, "MyMember2", ref refMyClass2);
            Type refMyClass2Type = refMyClass2.GetType();


            object deserializeMyClass2 = null;
            deserializeMyClass2 = JsonConvert.DeserializeObject(strMyClass2, refMyClass2Type);
            NewtonsoftJsonTest.SetMemberValue(myClass1, "MyMember2", deserializeMyClass2, refMyClass2Type);


            #endregion



            List<string> videogames = new List<string>
            {
                "Starcraft",
                "Halo",
                "Legend of Zelda"
            };

            string listjson = JsonConvert.SerializeObject(videogames);

            List<string> newVideogames = JsonConvert.DeserializeObject<List<string>>(listjson);


            Console.WriteLine(listjson);

            Console.WriteLine("Main End.");
        }



        public static void CopyDirFiles(string sourceDir, string destDir)
        {
            try
            {
                if (!Directory.Exists(sourceDir) || !Directory.Exists(destDir))
                {
                    return;
                }
                string[] files = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
                int index = sourceDir.Length;
                foreach (string sourceFileName in files)
                {
                    string filename = sourceFileName.Substring(index);
                    string to = destDir + filename;
                    string toDir = Path.GetDirectoryName(to);
                    if (!Directory.Exists(toDir))
                    {
                        Directory.CreateDirectory(toDir);
                    }
                    File.Copy(sourceFileName, to, true);
                }

            }
            catch (Exception ex)
            {
                string strMsg = string.Format("CopyDirFiles failed :{0}", ex.ToString());
            }

        }
    }


}
